import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import 'controller/train_teacher_degree_of_statisfaction_item_controller.dart';

///教师的培训
class TrainTeacherDegreeOfStatisFactionItemPage extends StatefulWidget {
  final TrainTeacherEntity trainTeacherEntity;

  const TrainTeacherDegreeOfStatisFactionItemPage(
      {Key key, @required this.trainTeacherEntity})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainTeacherDegreeOfStatisFactionItemPageState();
  }
}

class _TrainTeacherDegreeOfStatisFactionItemPageState
    extends State<TrainTeacherDegreeOfStatisFactionItemPage>
    with SingleTickerProviderStateMixin {
  TrainTeacherDegreeOfStatisfactionItemController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(
      TrainTeacherDegreeOfStatisfactionItemController(
          widget.trainTeacherEntity),
      tag: widget.trainTeacherEntity.trainId.toString(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("教师满意度测评"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.trainTeacherEntity.trainId.toString(),
        builder: (TrainTeacherDegreeOfStatisfactionItemController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildListView());
  }

  buildListView() {
    List<Map<String, dynamic>> list = _checkRecoderController.list;
    return Column(
      children: [
        Expanded(
          child: ListView.separated(
            itemCount: list.length,
            itemBuilder: (BuildContext context, int index) {
              return TrainDegreeOfStatisFactionItemWidgetPage(
                trainId: widget.trainTeacherEntity.trainId,
                map: list[index],
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider();
            },
          ),
        ),
        Container(
          padding: EdgeInsets.all(12),
          alignment: Alignment.center,
          child: Row(
            children: [
              Expanded(
                  child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    "当前评分总分为${_checkRecoderController.getTotalScore()}分",
                    style: TextStyle(fontWeight: FontWeight.w600),
                  ),
                  Text(
                    _checkRecoderController.getNoTotalNumber() == 0
                        ? "已全部评分完成 请点击提交"
                        : "当前还有${_checkRecoderController.getNoTotalNumber()}项未评分",
                    style: TextStyle(fontWeight: FontWeight.w600),
                  )
                ],
              )),
              Container(
                child: ElevatedButton(
                  onPressed: () {
                    if (_checkRecoderController.getNoTotalNumber() != 0) {
                      ToastUtils.showToast("请完成所有评分选项");
                      return;
                    }
                    _checkRecoderController
                        .commitTrainDegreeOfStatisfactionRequest(click: true);
                  },
                  child: GetBuilder(
                    tag: widget.trainTeacherEntity.trainId.toString(),
                    builder: (TrainTeacherDegreeOfStatisfactionItemController
                        controller) {
                      bool submitLoading = controller.getIsLoading();
                      if (submitLoading) {
                        return LoadingWidget();
                      }
                      return Text("提交");
                    },
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}

class TrainDegreeOfStatisFactionItemWidgetPage extends StatefulWidget {
  final Map<String, dynamic> map;
  final int trainId;

  const TrainDegreeOfStatisFactionItemWidgetPage(
      {Key key, @required this.map, this.trainId})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainDegreeOfStatisFactionItemWidgetState();
  }
}

class _TrainDegreeOfStatisFactionItemWidgetState
    extends State<TrainDegreeOfStatisFactionItemWidgetPage> {
  @override
  Widget build(BuildContext context) {
    String key = widget.map["key"];
    List<Map<String, dynamic>> values = widget.map["values"];
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            child: Text(
              key,
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: values.length,
            itemBuilder: (BuildContext context, int index) {
              return TrainDegreeOfStatisFactionItem2WidgetPage(
                trainId: widget.trainId,
                map: values[index],
              );
            },
          )
        ],
      ),
    );
  }
}

class TrainDegreeOfStatisFactionItem2WidgetPage extends StatefulWidget {
  final Map<String, dynamic> map;
  final int trainId;

  const TrainDegreeOfStatisFactionItem2WidgetPage(
      {Key key, @required this.map, this.trainId})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainDegreeOfStatisFactionItem2WidgetState();
  }
}

class _TrainDegreeOfStatisFactionItem2WidgetState
    extends State<TrainDegreeOfStatisFactionItem2WidgetPage> {
  @override
  void initState() {
    super.initState();
    String selectValue = widget.map["selectValue"];
    if (selectValue.isNotEmpty) {
      groupValue = int.parse(selectValue);
    }
  }

  @override
  Widget build(BuildContext context) {
    String key = widget.map["key"];

    List<int> values = widget.map["values"];
    List<Widget> rowList = [];
    for (var value in values) {
      rowList.add(_radio(value));
    }
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            child: Row(
              children: [
                Expanded(
                  child: Text(key),
                ),
                buildShowTipsWidget(),
              ],
            ),
          ),
          Wrap(
            children: rowList,
          ),
        ],
      ),
    );
  }

  buildShowTipsWidget() {
    String selectValue = widget.map["selectValue"];
    if (selectValue == null || selectValue.isEmpty) {
      return SizedBox();
    }
    return Text(
      "$selectValue分",
      style: TextStyle(fontWeight: FontWeight.bold, color: Colors.green),
    );
  }

  int groupValue = -1;

  Widget _radio(int index) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Radio(
            value: index,
            groupValue: groupValue,
            onChanged: (index) {
              groupValue = index;
              widget.map["selectValue"] = index.toString();
              Get.find<TrainTeacherDegreeOfStatisfactionItemController>(
                      tag: widget.trainId.toString())
                  .update(["root"]);
              setState(() {});
            }),
        Text("$index分")
      ],
    );
  }
}
